#include"process.h"

#define SIZE 101
#define MAX_RATE 100
#define STYLE '#'
#define STIME 1000*40

const char *str="|/-\\";
//
//void process_v1()
//{
//	//printf("hello process\n");
//	int rate=0;
//	//char bar[SIZE] = {0};
//	char bar[SIZE];
//	memset(bar,'\0',sizeof(bar));
//	int num = strlen(str);
//
//	while(rate <= MAX_RATE)
//	{
//	 	printf("[%-100s][%d%%][%c]\r",bar,rate,str[rate%num]);
//		fflush(stdout);
//		usleep(STIME);
//		bar[rate++] = STYLE;
//	} 

//	printf("\n");
//
//
//}
//
//不能一次将进度条打印完毕，否则无法平滑的和场景结合
//该函数，应该根据rate，自动的打一次
void process_v2(int rate)
{
	//version 2
	//TODO
	static char bar[SIZE] = {0};

	int num = strlen(str);

	
  	if(rate <= MAX_RATE && rate >= 0)
		{
	 		printf("[%-100s][%d%%][%c]\r",bar,rate,str[rate%num]);
			fflush(stdout);
			bar[rate] = STYLE;
		}
	if(rate == MAX_RATE) memset(bar,'\0',sizeof(bar));
}



void process_v3(double rate)
{
	//version 3
	//TODO
	static char bar[SIZE] = {0};
	static int cnt = 0;
	int num = strlen(str);

	
  	if(rate <= MAX_RATE && rate >= 0)
		{
			cnt++;
			cnt = (cnt >= num ? 0 : cnt);
	 		printf("[%-100s][%.1f%%][%c]\r",bar,rate,str[cnt]);
			fflush(stdout);
			if(rate < MAX_RATE)
			{
				
				bar[(int)rate] = STYLE_BODY;
				bar[(int)rate+1] = STYLE_HEADER;
			}
			else
			{
				bar[(int)rate] = STYLE_BODY;
			}
		}
	//if(rate == MAX_RATE) memset(bar,'\0',sizeof(bar));
}
